/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package daos;

import beans.Bean;
import beans.TurnoBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Milene
 */
public class MSSQLTurnoDao extends MSSQLDao implements Dao {

    public MSSQLTurnoDao() throws Exception {
        super();
    }

    @Override
    public Bean make(ResultSet result) throws Exception {
       
        TurnoBean tb = new TurnoBean();
        tb.setEstadoTurno(result.getString("estado"));
        tb.setFechaHora(result.getDate("fecha_hora"));
        tb.setEspecialidad(result.getString("especialidad"));
        tb.setNombreProfesional(result.getString("nombre_profesional"));
        tb.setApellidoProfesional(result.getString("apellido_profesional"));
        tb.setCobertura(result.getString("cobertura"));
        tb.setConsultorio(result.getString("consultorio"));
        tb.setCosto(result.getFloat("costo"));
        tb.setEstado(result.getString("estado"));
        tb.setIdPaciente(result.getInt("id_paciente"));
        tb.setSeguro(result.getString("seguro"));
        tb.setTiempo(result.getString("tiempo"));
        tb.setTipoCantidad(result.getString("tipo_cantidad"));
        return tb;
    }

 

    @Override
    public int update(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int delete(Bean bean) throws Exception {
        int rs=0;
        try {
            this.getConnection().setAutoCommit(false);//para mantener la integridad de la base, usar transaccion

            this.setStatement("desasignar_turno(?)");
            this.getStatement().setInt(1, ((TurnoBean) bean).getIdTurno());
            this.getStatement().executeUpdate();            
            this.getConnection().commit();

            
        } catch (SQLException ex) {
             rs=-1;
            this.getConnection().rollback();
            throw ex;           
        } catch (Exception ex2) {
             rs=-2;
            this.getConnection().rollback();
            throw ex2;
        } finally {
            this.getConnection().setAutoCommit(true);
        }
        return rs;
    }

    @Override
    public List<Bean> select(Bean bean) throws Exception {
        ResultSet result;
        List<Bean> turnos=new ArrayList<Bean>();
        this.setStatement("get_turno_paciente(?)");
        this.getStatement().setInt(1,((TurnoBean)bean).getIdPaciente());
        result=this.getStatement().executeQuery();
        while(result.next()){            
            turnos.add(this.make(result));
        }        
        return turnos;
    }           

    @Override
    public boolean valid(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int insert(Bean bean) throws Exception {
        try
        {
            this.getConnection().setAutoCommit(false);
            this.setStatement("asignar_turno (?, ?)");
            this.getStatement().setInt(1, ((TurnoBean) bean).getIdTurno());
            this.getStatement().setInt(2, ((TurnoBean) bean).getIdPaciente());            
            this.getStatement().executeUpdate();           
        } catch (SQLException ex) {
            this.getConnection().rollback();
            throw ex;
        } catch (Exception ex2) {
            this.getConnection().rollback();
            throw ex2;
        } finally {
            this.getConnection().setAutoCommit(true);
        }
        return 0;
    }

    @Override
    public Bean get(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public long getSize(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }    

   
}
